package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.Voice;
import com.tbit.uqbike.object.pojo.dto.VoiceDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.VoiceService;
import com.tbit.uqbike.webmanager.util.FileLoadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;
import java.util.Optional;

/**
 * 自定义语音Controller
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024/3/14
 */
@RestController
@RequestMapping("/voice")
public class VoiceController {

    @Resource
    private VoiceService voiceService;


    /**
     * 分页查询语音列表
     *
     * @param voiceDTO voiceDTO
     * @return Result<PageInfo < Voice>>
     */
    @GetMapping("/queryPage")
    public Result<PageResult<Voice>> queryPage(@Valid VoiceDTO voiceDTO) {
        return voiceService.queryPage(voiceDTO);
    }

    /**
     * 新增语音
     *
     * @param brandId   品牌id
     * @param accountId 区域id
     * @param voiceName 语音名称
     * @param delFlag   上架状态(0=上架 1=下架)
     * @param file      语音文件
     * @return result
     */
    @PostMapping("add")
    @OperateLog(module = ModuleEnum.VOICE_CUSTOM, methods = MethodEnum.VOICE_CUSTOM_ADD)
    public Result<Object> add(Integer brandId, Integer accountId, String voiceName, Integer delFlag, MultipartFile file, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (Objects.isNull(brandId)) {
            return Result.error("品牌id不能为空");
        }
        if (Objects.isNull(accountId)) {
            return Result.error("请选择区域");
        }
        if (Objects.isNull(voiceName)) {
            return Result.error("请输入语音名称");
        }
        if (Objects.isNull(delFlag)) {
            return Result.error("请确认上架状态");
        }
        if (Objects.isNull(file)) {
            return Result.error("请上传语音文件");
        }
        String fileSuffix = Optional.ofNullable(file.getOriginalFilename())
                .filter(StringUtils::isNotBlank)
                .filter(x -> x.lastIndexOf(".") != -1)
                .map(x -> x.substring(x.lastIndexOf(".") + 1))
                .orElse(null);
        String onlyPdfSuffix = "MP3";
        if (!onlyPdfSuffix.equalsIgnoreCase(fileSuffix)) {
            return Result.error("请上传MP3文件");
        }
        //todo 调试中
//        if (!FileLoadUtils.checkFileSize(file, 2, "M")) {
//            return Result.error("语音文件大小不能超过2M,请处理后重新上传");
//        }
        return voiceService.add(brandId, accountId, voiceName, delFlag, file, login);
    }

    /**
     * 修改语音
     *
     * @param id        id
     * @param brandId   品牌id
     * @param accountId 区域id
     * @param voiceName 语音名称
     * @param delFlag   上架状态(0=上架 1=下架)
     * @param file      语音文件
     * @return result
     */
    @PostMapping("/update")
    @OperateLog(module = ModuleEnum.VOICE_CUSTOM, methods = MethodEnum.VOICE_CUSTOM_UPDATE)
    public Result<Object> update(Integer id, Integer brandId, Integer voiceId, Integer accountId, String voiceName, Integer delFlag, MultipartFile file) {
        if (Objects.isNull(id)) {
            return Result.error("id不能为空");
        }
        if (Objects.isNull(accountId)) {
            return Result.error("请选择区域");
        }
        if (Objects.isNull(voiceName)) {
            return Result.error("请输入语音名称");
        }
        if (Objects.isNull(delFlag)) {
            return Result.error("请确认上架状态");
        }
        if (Objects.nonNull(file)) {
            String fileSuffix = Optional.ofNullable(file.getOriginalFilename())
                    .filter(StringUtils::isNotBlank)
                    .filter(x -> x.lastIndexOf(".") != -1)
                    .map(x -> x.substring(x.lastIndexOf(".") + 1))
                    .orElse(null);
            String onlyPdfSuffix = "MP3";
            if (!onlyPdfSuffix.equalsIgnoreCase(fileSuffix)) {
                return Result.error("请上传MP3文件");
            }
        }
        if (Objects.nonNull(file) && !FileLoadUtils.checkFileSize(file, 2, "M")) {
            return Result.error("语音文件大小不能超过2M,请处理后重新上传");
        }
        return voiceService.update(id, brandId, voiceId, accountId, voiceName, delFlag, file);
    }


    /**
     * 删除语音
     *
     * @param id id
     * @return Result<Object>
     */
    @DeleteMapping("/delete")
    public Result<Object> delete(Integer id) {
        if (Objects.isNull(id)) {
            return Result.error("请选择一条语音删除");
        }
        return voiceService.deleteId(id);
    }


}
